import { useContext, useRef } from 'react'
import { CanvasContext } from '../Context'
import CanvasStore from './canvas'

// 在整个组件生命周期类有效,使用ref
// 并且保持唯一,外部引用内部值产生闭包
export function useCanvas(canvas) {
	const canvasRef = useRef()
	if (!canvasRef.current) {
		if (canvas) {
			canvasRef.current = canvas
		} else {
			canvasRef.current = new CanvasStore().getPublicCanvas()
		}
	}
	return canvasRef.current
}

// 利用context获取全局状态类
export function useCanvasContext() {
	const canvs = useContext(CanvasContext)
	return canvs
}
// 获取画布数据
export function useCanvasData() {
	const canvs = useContext(CanvasContext)
	return canvs.getCanvas()
}
// 获取画布组件列表
export function useCanvasCmps() {
	const canvas = useContext(CanvasContext)
	return canvas.getCanvasCmps()
}
